// pages/category/index.js
import { request } from "../../request/request.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    leftMenuList:[],
    rightContent:[],
    currentIndex:0
   

  },
  cates: [],


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

 const cates=wx.getStorageSync("cates")
 if(!cates)
 {
   this.getCategory();

 }
 else{
      if(Date.now()-cates.time>10000){
        this.getCategory();
      }
      else{
        this.cates=cates.date;
        
        let leftMenuList = this.cates.map(v => v.cat_name)
        let rightContent = this.cates[0].children
        this.setData({
          leftMenuList: leftMenuList,
          rightContent: rightContent
        })

      }
 }
  },
  getCategory() {
    request({ url: "/categories" }).then(result => {
      console.log(result);
      this.cates=result.data.message;
      wx.setStorageSync("cates",{time:Date.now(),date:this.cates});
     
      let leftMenuList = this.cates.map(v=>v.cat_name);
      let rightContent=this.cates[0].children;
      this.setData({
        leftMenuList:leftMenuList,
        rightContent: rightContent
      })

    })
  },
  handleItemTap(e){

const index=e.currentTarget.dataset.index;
    let rightContent = this.cates[index].children;
this.setData({
  currentIndex:index,
  rightContent: rightContent
})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})